﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UI
{
    public partial class FrmMapImage : Form, MapWinGIS.ICallback 
    {

        int ActiveLayerHandle = 0;
        public event EventHandler PlayAnimation;

        int MapMax = int.MaxValue;
        int MapMin = int.MinValue;

        public FrmMapImage(string text)
        {
            InitializeComponent();

           
           

            this.Text = text;
        }
        public void Set_LayerName(int LayerHandle, string Label)
        {
            axMap1.set_LayerName(LayerHandle, Label);
        }
        public void Error(string KeyOfSender, string ErrorMsg)
        { 
        
        }

        public void Progress(string KeyOfSender, int Percent, string Message)
        {
            toolStripProgressBar1.Value = Percent;

        }

        public int LayerCount
        {
            get
            {
                 
                return axMap1.NumLayers;
            }
        }
        public int get_LayerHandle(int position)
        {
            return axMap1.get_LayerHandle(position);
        }
        public void set_LayerVisible(int LayerHandle, bool value)
        {
            axMap1.set_LayerVisible(LayerHandle, value);
        }
        public string LayerName(int LayerHandle)
        {
            return axMap1.get_LayerName(LayerHandle);
        }
        
        public void ZoomToMaxExtents()
        {
            axMap1.ZoomToMaxExtents();
        }
        public MapWinGIS.Grid get_grid(int LayerHandle)
        {
            string FileName = axMap1.get_GridFileName(LayerHandle);

            MapWinGIS.Grid grid = new MapWinGIS.Grid();

            grid.Open(FileName, MapWinGIS.GridDataType.LongDataType, true, MapWinGIS.GridFileType.UseExtension, this);

            return grid;
        }
        public MapWinGIS.Image GridToImage(MapWinGIS.Grid grid, MapWinGIS.GridColorScheme _GridColorscheme)
        {
            return new MapWinGIS.UtilsClass().GridToImage(grid, _GridColorscheme, this);
        }
         

        public void set_Image(MapWinGIS.Image image)
        {
            int LayerHandle = axMap1.AddLayer(image, true);

            Set_LayerName(LayerHandle, "");

            SetImageLayerColorScheme(LayerHandle, GridColorscheme);

           
        }
        public bool SetImageLayerColorScheme(int LayerHandle, MapWinGIS.GridColorScheme gridScheme)
        {
            return  axMap1.SetImageLayerColorScheme(LayerHandle, gridScheme);
          
        }
        public static IColorScheme GetColorScheme(int BinWidth)
        {
            if (BinWidth == 1) return new ColorScheme(ColorScheme.DefaultColorValues);
            else return new ColorSchemeClassified(new byte[] { 0, 255 }, new byte[] { 0, 255 }, new byte[] { 255, 255 }, 10);
        }
        public static int GetBinWidth(int MapMin, int MapMax)
        {
            int Range = MapMax - MapMin;
            if (Range > 10) return (int)(Range / 10);
            else return 1;
        }
        public MapWinGIS.GridColorScheme GetGridScheme(MapWinGIS.GridColorBreak[] colorbreaks, IColorScheme color_scheme)
        {
            MapWinGIS.GridColorScheme grid_scheme = new MapWinGIS.GridColorScheme();

            foreach(MapWinGIS.GridColorBreak colorbreak in colorbreaks)
            {
                grid_scheme.InsertBreak(colorbreak);
            }
            
            return grid_scheme;
        }
        public MapWinGIS.GridColorScheme GetGridScheme(int MapMin, int MapMax, int BinWidth, IColorScheme color_scheme)
        {
            MapWinGIS.GridColorScheme gridScheme = new MapWinGIS.GridColorScheme();

            float i = MapMin;
            while (i <= MapMax)
            {
                MapWinGIS.GridColorBreak colorbreak = new MapWinGIS.GridColorBreak();

                colorbreak.LowValue = i;
                colorbreak.HighValue = i + BinWidth;

                System.Drawing.Color mycolor = color_scheme.NextColor;

                colorbreak.HighColor = colorbreak.LowColor = mycolor.ToUInt();

                gridScheme.InsertBreak(colorbreak);
                 
                i += BinWidth;
            }

            return gridScheme;
        }

        public MapWinGIS.GridColorScheme GridColorscheme
        {
            get;
            private set;
        }

        public void AddLayer(string FileName, string LayerName)
        {
            MapWinGIS.Grid grid = new MapWinGIS.Grid();
            
            grid.Open(FileName, MapWinGIS.GridDataType.LongDataType, true, MapWinGIS.GridFileType.UseExtension, this);

            MapMin = Math.Max(0, Math.Min(MapMin, int.Parse(grid.Minimum.ToString())));
            MapMax = Math.Min(MapMax, int.Parse(grid.Maximum.ToString()));

            int BinWidth = GetBinWidth(MapMin, MapMax);

            IColorScheme Colorscheme = GetColorScheme(BinWidth);

            GridColorscheme = GetGridScheme(MapMin, MapMax, BinWidth, Colorscheme);

            toolStripStatusLabel1.Text  = "Loading " + FileName;

            statusStrip1.Invalidate();
            statusStrip1.Update();
            statusStrip1.Refresh();

            MapWinGIS.Image image = new MapWinGIS.UtilsClass().GridToImage(grid, GridColorscheme, this);

            toolStripStatusLabel1.Text = "Ready";

            image.CustomColorScheme = GridColorscheme;

            grid.Close();

            int LayerHandle = axMap1.AddLayer(image, true);

            Set_LayerName(LayerHandle, LayerName);

            SetImageLayerColorScheme(LayerHandle, GridColorscheme);

            ZoomToMaxExtents();

            this.axMap1.Invalidate();
            this.axMap1.Update();
            this.axMap1.Refresh();
          
        }
        
        
        private void axMap1_MouseMoveEvent(object sender, AxMapWinGIS._DMapEvents_MouseMoveEvent e)
        {
           
        }

        double MaxX(MapWinGIS.Grid grid, out int NrOfCols)
        {
            double X0 = 0;
            double Y0;

            NrOfCols = 0;
            double pixelValue = 0;
            while (pixelValue >= 0)
            {
                grid.CellToProj(NrOfCols,0 , out X0, out Y0);
                pixelValue = (double)grid.get_Value(NrOfCols, 0);
                NrOfCols++;
            }
            return X0;
        }
         
        double MaxY(MapWinGIS.Grid grid, out int NrRows)
        {
            double X0 ;
            double Y0 = 0;

            NrRows = 0;
            double pixelValue = 0;
            while (pixelValue >= 0)
            {
                grid.CellToProj(0, NrRows, out X0, out Y0);
                pixelValue = (double)grid.get_Value(0, NrRows);
                NrRows++;
            }
            return Y0;
        }
        private void axMap1_MouseDownEvent(object sender, AxMapWinGIS._DMapEvents_MouseDownEvent e)
        {
            if (axMap1.CursorMode != MapWinGIS.tkCursorMode.cmSelection) return;

            double PX = 0;
            double PY = 0;

            axMap1.PixelToProj(e.x, e.y, ref PX, ref PY);

            MapWinGIS.Grid myGrid = get_grid(ActiveLayerHandle);
 
            int c, r;
            myGrid.ProjToCell(PX, -PY, out c, out r);
            double LandisValue = (double)myGrid.get_Value(c, r);
            string landis = (LandisValue <0) ? "n/a" : LandisValue.ToString();

            myGrid.ProjToCell(PX, PY, out c, out r);
            double ProjectedValue = (double)myGrid.get_Value(c, r);
            string projected = (ProjectedValue <0 )? "n/a" : ProjectedValue.ToString();

            toolStripGridValue.Text = toolStripGridValue.Tag + landis.ToString() + "|" + projected.ToString();
        }

       

        
        private void EventPlayAnimation(object sender, EventArgs e)
        {
            if (this.PlayAnimation != null)
            {
                this.PlayAnimation(new object(), e);
            }
        }

        private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            string tag = (string)e.Button.Tag;
            switch (tag)
            {
                case "ZoomIn":
                    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomIn;
                    break;
                case "ZoomOut":
                    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomOut;
                    break;
                case "Pan":
                    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmPan;
                    break;
                case "FullExtents":
                    axMap1.ZoomToMaxExtents();
                    break;
                case "Animation":
                    EventPlayAnimation(this, new EventArgs());
                    break;
                case "Info":
                    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmSelection;
                    break;
                 

            }
        }

        private void toolStripProgressBar1_Click(object sender, EventArgs e)
        {

        }

        
        

        
    }
}
